﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.IO;

namespace GVCSMTemplates
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public String imageSpeed;
        public String imageTimeout;
        public String imageTransitionEffect;
        public String textSpeed;
        public String textTimeout;
        public String textTransitionEffect;
        public String filmTitleFontColor;
        public String filmTitleFontSize;
        public String filmTitleFontType;
        public String filmRatingFontColor;
        public String filmRatingFontSize;
        public String filmRatingFontType;
        public String movieSessionTimingFontSize;
        public String movieSessionTimingFontColorAvailable;
        public String movieSessionTimingFontColorSellingFast;
        public String movieSessionTimingFontColorSoldOut;
        public String movieSessionTimingFontType;
        public String tableName;

        IniFile ini = new IniFile("c://Box/Signbox/settings.ini");

        CalculateTableInformationWithoutImage ct;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadDefaultTransitionSettings();
                loadFilmInformationSettings();
                retrieveUICode();
                checkForAMPM();
                timerRefresh.Enabled = true;
            }
        }

        public void loadDefaultTransitionSettings()
        {
            tableName = ini.ReadValue("Table Information", "tableName");
            imageSpeed = ini.ReadValue("Four Screen Template", "imageSpeed");
            imageTimeout = ini.ReadValue("Four Screen Template", "imageTimeout");
            imageTransitionEffect = ini.ReadValue("Four Screen Template", "imageTransitionEffect");

            textSpeed = ini.ReadValue("Four Screen Template", "textSpeed");
            textTimeout = ini.ReadValue("Four Screen Template", "textTimeout");
            textTransitionEffect = ini.ReadValue("Four Screen Template", "textTransitionEffect");
        }

        public void loadFilmInformationSettings()
        {
            filmTitleFontColor = ini.ReadValue("Four Screen Template - Movie Information", "FilmTitleFontColor");
            filmTitleFontSize = ini.ReadValue("Four Screen Template - Movie Information", "FilmTitleFontSize");
            filmTitleFontType = ini.ReadValue("Four Screen Template - Movie Information", "FilmTitleFontType");
            filmRatingFontColor = ini.ReadValue("Four Screen Template - Movie Information", "FilmRatingFontColor");
            filmRatingFontSize = ini.ReadValue("Four Screen Template - Movie Information", "FilmRatingFontSize");
            filmRatingFontType = ini.ReadValue("Four Screen Template - Movie Information", "FilmRatingFontType");

            movieSessionTimingFontSize = ini.ReadValue("Four Screen Template - Movie Information", "MovieSessionTimingFontSize");
            movieSessionTimingFontColorAvailable = ini.ReadValue("Four Screen Template - Movie Information", "MovieSessionTimingFontColorAvailable");
            movieSessionTimingFontColorSellingFast = ini.ReadValue("Four Screen Template - Movie Information", "MovieSessionTimingFontColorSellingFast");
            movieSessionTimingFontColorSoldOut = ini.ReadValue("Four Screen Template - Movie Information", "MovieSessionTimingFontColorSoldOut");
            movieSessionTimingFontType = ini.ReadValue("Four Screen Template - Movie Information", "MovieSessionTimingFontType");
        }

        public void retrieveUICode()
        {
            IniFile ini = new IniFile(@"C:\signPlayer.ini");
            ct = new CalculateTableInformationWithoutImage();
            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\BOX\SIGNBOX\gv-box.mdb;Jet OLEDB:Database Password=";
            string sql2 = "select * from Sign_GVPlazaNormalTemplate";
            OleDbConnection conns = new OleDbConnection(connString);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = new OleDbCommand(sql2, conns);

            DataSet ds = new DataSet();
            da.Fill(ds, "Sign_GVPlazaNormalTemplate");

            if (!(ds.Tables[0].Rows.Count <= 0))
            {
                DataTable dt = ds.Tables["Sign_GVPlazaNormalTemplate"];
                try
                {
                    DataTable dtWithoutImageRowsQ1 = new DataTable();
                    DataTable dtWithoutImageRowsQ2 = new DataTable();
                    DataTable dtWithoutImageRowsQ3 = new DataTable();
                    DataTable dtWithoutImageRowsQ4 = new DataTable();
                    dtWithoutImageRowsQ1 = dt.Clone();
                    dtWithoutImageRowsQ1.Clear();
                    dtWithoutImageRowsQ2 = dt.Clone();
                    dtWithoutImageRowsQ2.Clear();
                    dtWithoutImageRowsQ3 = dt.Clone();
                    dtWithoutImageRowsQ3.Clear();
                    dtWithoutImageRowsQ4 = dt.Clone();
                    dtWithoutImageRowsQ4.Clear();

                    dtWithoutImageRowsQ1 = ct.retrieveTableForWithoutImageScreenTemplateQ1(dt);
                    String movieInformationHTMLQuarter1 = displayMovieInformation(dtWithoutImageRowsQ1);
                    OutputMovieTitle.Text = movieInformationHTMLQuarter1 + "</table></div></div>";

                    dtWithoutImageRowsQ2 = ct.retrieveTableForWithoutImageScreenTemplateQ2(dt);
                    String movieInformationHTMLQuarter2 = displayMovieInformation(dtWithoutImageRowsQ2);
                    Literal1.Text = movieInformationHTMLQuarter2 + "</table></div></div>";

                    dtWithoutImageRowsQ3 = ct.retrieveTableForWithoutImageScreenTemplateQ3(dt);
                    String movieInformationHTMLQuarter3 = displayMovieInformation(dtWithoutImageRowsQ3);
                    Literal2.Text = movieInformationHTMLQuarter3 + "</table></div></div>";

                    dtWithoutImageRowsQ4 = ct.retrieveTableForWithoutImageScreenTemplateQ4(dt);
                    String movieInformationHTMLQuarter4 = displayMovieInformation(dtWithoutImageRowsQ4);
                    Literal4.Text = movieInformationHTMLQuarter4 + "</table></div></div>";

                    //String movieInformationHTMLQuarter1 = displayMovieInformation(dt);
                    //OutputMovieTitle.Text = movieInformationHTMLQuarter1 + "</table></div></div>";

                    //int rowCount = dt.Rows.Count;
                    //DataTable dt1 = SwitchDataTableRows(dt, 0, rowCount - 1);
                    //String movieInformationHTMLQuarter2 = displayMovieInformation(dt1);
                    //Literal1.Text = movieInformationHTMLQuarter2 + "</table></div></div>";

                    //if (rowCount > 4)
                    //{
                    //    //DataTable dt2 = SwitchDataTableRows(dt, 0, rowCount - 1);
                    //    //String movieInformationHTMLQuarter3 = displayMovieInformation(dt2);
                    //    //Literal2.Text = movieInformationHTMLQuarter3 + "</table></div></div>";

                    //    //if (rowCount > 5)
                    //    //{
                    //    //    DataTable dt3 = SwitchDataTableRows(dt, 0, rowCount - 1);
                    //    //    String movieInformationHTMLQuarter4 = displayMovieInformation(dt3);
                    //    //    Literal4.Text = movieInformationHTMLQuarter4 + "</table></div></div>";
                    //    //}
                    //    conns.Close();
                    //}
                }

                catch (System.Exception ex)
                {
                    Response.Write("No Data" + ex.ToString());
                }
            }
            else
            {
                IniFile texttime = new IniFile(@"C:\BOX\SIGNBOX\testtime.ini");
                texttime.WriteValue("Sign_GVPlazaNormalTemplate", "time", DateTime.Now.ToString());
            }
        }

        private DataTable SwitchDataTableRows(DataTable dt, int i, int j)
       {
            
            for (int ii = i; ii <= j; ii++)
            {
                int jj = j;
                object[] iRow = dt.Rows[ii].ItemArray;
                object[] jRow = dt.Rows[jj].ItemArray;
                dt.Rows[ii].ItemArray = jRow;
                dt.Rows[jj].ItemArray = iRow;
                jj = jj - 1;
            }
            return dt;
        }

        public String displayMovieInformation(DataTable dt)
        {
            string strOutput = "";
            strOutput = "<div class='Textslideshow'>";

            int V1 = 0;
            string V2 = "";

            foreach (DataRow row in dt.Rows)
            {
                if (V1 == 0)
                {
                    V2 = "<div><table width='100%'>";
                }
                else if (V1 % 1 == 0)
                {
                    V2 = "</table></div><div><table width='100%'>";
                }
                else
                {
                    V2 = "";
                }

                strOutput += V2 + "<tr>";

                strOutput += "<td colspan='3' width='2000px' align='left' valign='top'>";

                strOutput += "<B><font color='White' style= \"font-family:" + filmTitleFontType + ";font-size:" + filmTitleFontSize + ";color" + filmTitleFontColor + "; font-weight:normal; \">" + "  " + row["Film_Title"].ToString() + "</font></B></td></tr><tr><td height='5px'></td></tr>";
                strOutput += "<tr><td width='15%' align='LEFT' valign='bottom'> <font size='" + filmRatingFontSize + "' font color='" + filmRatingFontColor + "' style= \"font-family:Futura Bk BT;\" color:" + filmRatingFontColor + "; >" + "  " + row["Rating"].ToString() + "</font></td></tr><tr><td height='10px'></td></tr>";

                //strOutput += "<tr><td class='RatingTemplateTextStyle' width='15%' align='left' valign='top'>" + row["Rating"].ToString() + "</td></tr><tr><td height='25px'></td></tr>";
                strOutput += "<tr><td width='100%' align='left' valign='left' style=\"font-family:@Futura Bk BT\"><table border=0 cellpadding=3 cellspacing=3><tr><td width='20%' align='left'>"

                + SessionStatus(row["Status1"].ToString(), row["Session_Time1"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status2"].ToString(), row["Session_Time2"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status3"].ToString(), row["Session_Time3"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status4"].ToString(), row["Session_Time4"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status5"].ToString(), row["Session_Time5"].ToString()) + "</td></tr><tr><td width='20%' align='left'>"

                + SessionStatus(row["Status6"].ToString(), row["Session_Time6"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status7"].ToString(), row["Session_Time7"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status8"].ToString(), row["Session_Time8"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status9"].ToString(), row["Session_Time9"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status10"].ToString(), row["Session_Time10"].ToString()) + "</td></tr><tr><td width='20%' align='left'>"

                + SessionStatus(row["Status11"].ToString(), row["Session_Time11"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status12"].ToString(), row["Session_Time12"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status13"].ToString(), row["Session_Time13"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status14"].ToString(), row["Session_Time14"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status15"].ToString(), row["Session_Time15"].ToString()) + "</td></tr><tr><td width='20%' align='left'>"

                + SessionStatus(row["Status16"].ToString(), row["Session_Time16"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status17"].ToString(), row["Session_Time17"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status18"].ToString(), row["Session_Time18"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status19"].ToString(), row["Session_Time19"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status20"].ToString(), row["Session_Time20"].ToString()) + "</td><td width='20%' align='left'></td></tr></table>" + "</td>";

                strOutput += "</tr>";

                V1 = V1 + 1;
            }
            return strOutput;
        }

       public String displayImages(DataTable dt)
        {
            string posterImg = "";

            foreach (DataRow row in dt.Rows)
            {
                if (row["Media_Name"].ToString() != "" && row["Media_Path"].ToString() != "")
                {
                    posterImg = posterImg + "<img src='" + @"C:\BOX\SIGNBOX\MEDIA_PLAYER\Picture\img" + row["SHO_FILM_CD"] + ".jpeg'/>";
                }
                else
                {
                    String posterVal = posterTags(row);
                    posterImg = posterImg + posterVal.ToString();
                }
                if (row["Session_Time16"].ToString() != "" && row["Status16"].ToString() != "")
                {
                    string displaySameMovieInfoImage = posterTags(row);
                    posterImg = posterImg + displaySameMovieInfoImage.ToString();
                }
            } return posterImg;
        }

        public string posterTags(DataRow row)
        {
            String posterImgHTMLTag = "";
            if (File.Exists(@"C:\BOX\SIGNBOX\MEDIA_PLAYER\Picture\img" + row["SHO_FILM_CD"].ToString() + ".jpg"))
            {
                posterImgHTMLTag = "<img src='" + @"http://localhost/images/img" + row["SHO_FILM_CD"] + ".jpg'/>";
            }
            else
            {
                posterImgHTMLTag = "<img src='" + @"http://localhost/images/" + "plazaDefault.jpg'/>";
            }
            return posterImgHTMLTag;
        }




        public string SessionStatus(string status, string time)
        {
            string strReturn = "";
            status = status.Replace(" ", "").ToUpper();
            if (status != "" && time != "")
            {
                switch (status)
                {
                    case "AVAILABLE":
                        strReturn = "<B><font color='" + movieSessionTimingFontColorAvailable + "' size='30px' style= \"font-family:" + movieSessionTimingFontType + "; font-size:" + movieSessionTimingFontSize + "; font-weight:bold;\" >" + time + "</font></B>";
                        break;
                    case "SELLINGFAST":
                        strReturn = "<B><font color='" + movieSessionTimingFontColorSellingFast + "' size='30px' style=\"font-family:" + movieSessionTimingFontType + "; font-size:" + movieSessionTimingFontSize + "; font-weight:bold;\" class=\"blk\" >" + time + "</font></B>";
                        break;
                    case "SOLDOUT":
                        strReturn = "<B><font color='" + movieSessionTimingFontColorSoldOut + "' size='30px' style=\"font-family:" + movieSessionTimingFontType + "; font-size:" + movieSessionTimingFontSize + "; font-weight:bold;\" >" + time + "</font></B>";
                        break;
                }

                return strReturn;
            }
            else
            {
                return strReturn;
            }
        }

        protected void timerRefresh_Tick(object sender, EventArgs e)
        {

            retrieveUICode();
        }

        public void checkForAMPM()
        {
            DateTime time = DateTime.Now;
            if (time.Hour > 12)
            {
                checkAMPM.Text = "PM";
            }
            else
            {
                checkAMPM.Text = "AM";
            }
        }
    }
}
 